home *** CD-ROM | disk | FTP | other *** search
- Path: locutus.rchland.ibm.com!usenet
- From: pstaite@vnet.ibm.com
- Newsgroups: comp.lang.c++
- Subject: Re: Using a Base constructor for a derived class
- Date: 16 Jan 1996 20:10:16 GMT
- Organization: IBM OS/2 Device Driver Development Rochester, MN
- Message-ID: <4dh0n8$19kh@locutus.rchland.ibm.com>
- References: <4ddui9$a14@felix.cc.gatech.edu>
- Reply-To: pstaite@vnet.ibm.com
- NNTP-Posting-Host: warpone.rchland.ibm.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4ddui9$a14@felix.cc.gatech.edu>, culbreth@cc.gatech.edu (Matthew W. Culbreth) writes:
- >Howdy,
- >
- >I have a base class with a constructor. This class is never used
- >in my application. I have three classes derived from this class.
- >
- >When I create an instant of one of the derived class, I want to use the
- >constructor from the base class. The compiler is stating that it can't find
- >the constructor for the arguments passed for any of the derived classes.
- >Are constructors inherited the same as member functions?
-
- No. For example:
-
- class foo {
- public:
- foo( int );
- };
-
- class bar : public foo {
- };
-
- Note, class bar has no constructor that takes an int, it has only the
- compiler-generated default constructor. However, even this won't
- compile :-(
-
- By default, all derived class constructors call the base class' default
- constructor. (ie. the parameterless one) However, class foo has no
- parameterless constructor. What you really need is:
-
- class bar : public foo {
- public:
- bar( int x ) : foo( x ) {}
- };
-
- This declares (and defines inline) an int constructor for bar. It also
- explicitly directs the compiler to use the int constructor for the base
- class (foo) part of the derived class (bar).
-
- So, you're derived classes will need to declare/define all "types" of
- constructors you want them to make available. In addition, if you want
- to use something other than the base class' default constructor, you'll
- have to have each derived constructor specify it in the
- constructor-initializer list.
-
-
- Phil Staite, team OS/2
- internet: pstaite@vnet.ibm.com internal: pstaite@rchland
-
-